<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/3/15 16:44
 * @version
 */
namespace App;

use Exception;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

use Illuminate\Support\Facades\Log;

class AliyunPush
{
    public static function push($query)
    {
        AlibabaCloud::accessKeyClient('LTAIr11HyTGrQcsC', 'za6PFk4mk8BqQOeHroy6n82Yi1QgtJ')
            ->regionId('cn-hangzhou') // replace regionId as you need
            ->asGlobalClient();

        try {
            $result = AlibabaCloud::rpcRequest()
                ->product('Push')
                // ->scheme('https') // https | http
                ->version('2016-08-01')
                ->action('Push')
                ->method('POST')
                ->options([
                    'query' => $query
                ])
                ->request();
            $res = $result->toArray();
            if(isset($res['MessageId'])) return $res['MessageId'];
        } catch (ClientException $e) {
            Log::error($e->getErrorMessage());
        } catch (ServerException $e) {
            Log::error($e->getErrorMessage());
        }
        return false;
    }

    public static function pushApple($deviceId,$title,$body,$params = null)
    {
        $appKey = '25809869';
        $deviceType = 'iOS';
        $query = [
            'AppKey' => $appKey,
            'DeviceType' => $deviceType,
            'PushType' => 'NOTICE',
            'Target' => 'DEVICE',
            'TargetValue' => $deviceId,
            'Body' => $body,
            'Title' => $title,
            'iOSExtParameters'=>$params ? json_encode($params) : '',
            'iOSApnsEnv'=>'PRODUCT'//PRODUCT|DEV
        ];
        return self::push($query);
    }

    public static function pushAndroid($deviceId,$title,$body,$params = null)
    {
        $appKey = '25811841';
        $deviceType = 'ANDROID';
        $query = [
            'AppKey' => $appKey,
            'DeviceType' => $deviceType,
            'PushType' => 'NOTICE',
            'Target' => 'DEVICE',
            'TargetValue' => $deviceId,
            'Body' => $body,
            'Title' => $title,
            'AndroidExtParameters'=>$params ? json_encode($params) : '',
        ];
        return self::push($query);
    }


}